package base2

import (
	"net/http"
	"time"
)

var BsfConfigInstance = &BsfConfig{}

type DbConfig struct {
	DriverName      string
	DataSourceName  string
	MaxPoolSize     int
	MinPoolSize     int
	ConnMaxLifetime time.Duration
}

type SessionConfig struct {
	SecretKey string
}

type WebConfig struct {
	WebName            string
	Developer          string
	Port               int
	Gzip               bool
	MaxRequestBodySize int64
}

type BsfConfig struct {
}

// DB 相关配置
func (*BsfConfig) GetDbEnabled() bool {
	return CoreUtil.ConfigGet("db.enabled", true).(bool)
}
func (*BsfConfig) GetDbSqlTimeLogEnabled() bool {
	return CoreUtil.ConfigGet("db.sqlTimeLog.enabled", true).(bool)
}
func (*BsfConfig) GetDbDriveName() string {
	return CoreUtil.ConfigGet("db.driverName", "mysql").(string)
}

func (*BsfConfig) GetDbDataSourceName() string {
	return CoreUtil.ConfigGet("db.dataSourceName", "root:root@tcp(127.0.0.1:3306)/test").(string)
}

func (*BsfConfig) GetDbMinPoolSize() int {
	return CoreUtil.ConfigGet("db.minPoolSize", 1).(int)
}

func (*BsfConfig) GetDbMaxPoolSize() int {
	return CoreUtil.ConfigGet("db.maxPoolSize", 20).(int)
}

func (*BsfConfig) GetDbConnMaxLifetime() time.Duration {
	return CoreUtil.ConfigGet("db.connMaxLifetime", time.Minute).(time.Duration)
}

// Web 相关配置
func (*BsfConfig) GetWebName() string {
	return CoreUtil.ConfigGet("web.webname", "demo").(string)
}

func (*BsfConfig) GetWebDeveloper() string {
	return CoreUtil.ConfigGet("web.developer", "").(string)
}

func (*BsfConfig) GetWebPort() int {
	return CoreUtil.ConfigGet("web.port", 8888).(int)
}

func (*BsfConfig) GetWebResponseDetailErrorEnabled() bool {
	return CoreUtil.ConfigGet("web.responseDetailError.enabled", true).(bool)
}

// Session 相关配置
func (m *BsfConfig) GetSessionEnabled() bool {
	return CoreUtil.ConfigGet("session.enabled", false).(bool)
}
func (m *BsfConfig) GetSessionMode() string {
	return CoreUtil.ConfigGet("session.mode", "cookie").(string)
}

func (m *BsfConfig) GetSessionSecretKey() string {
	return CoreUtil.ConfigGet("session.secretKey", m.GetWebName()).(string)
}

func (*BsfConfig) GetSessionDomain() string {
	return CoreUtil.ConfigGet("session.domain", "").(string)
}
func (*BsfConfig) GetSessionPath() string {
	return CoreUtil.ConfigGet("session.path", "/").(string)
}
func (*BsfConfig) GetSessionHttpOnly() bool {
	return CoreUtil.ConfigGet("session.httpOnly", true).(bool)
}

func (*BsfConfig) GetSessionSecure() bool {
	return CoreUtil.ConfigGet("session.secure", true).(bool)
}

func (*BsfConfig) GetSessionMaxAge() int {
	return CoreUtil.ConfigGet("session.maxAge", 86400).(int)
}

func (*BsfConfig) GetSessionSameSite() http.SameSite {
	return CoreUtil.ConfigGet("session.sameSite", http.SameSiteLaxMode).(http.SameSite)
}

func (*BsfConfig) GetSessionRedisMinPoolSize() int {
	return CoreUtil.ConfigGet("session.redis2.minPoolSize", 10).(int)
}

func (*BsfConfig) GetSessionRedisAddress() string {
	return CoreUtil.ConfigGet("session.redis2.address", "localhost:6379").(string)
}

func (*BsfConfig) GetSessionRedisUsername() string {
	return CoreUtil.ConfigGet("session.redis2.username", "").(string)
}

func (*BsfConfig) GetSessionRedisPassword() string {
	return CoreUtil.ConfigGet("session.redis2.password", "").(string)
}

// template 配置
func (*BsfConfig) GetTemplateEnabled() bool {
	return CoreUtil.ConfigGet("template.enabled", false).(bool)
}
func (*BsfConfig) GetTemplatePath() string {
	return CoreUtil.ConfigGet("template.path", "web/public/views/").(string)
}
func (*BsfConfig) GetTemplateErrorTemplate() string {
	return CoreUtil.ConfigGet("template.errorTemplate", "home/error.html").(string)
}

//func (*BsfConfig) GetTemplateErrorUrlPath() string {
//	return CoreUtil.ConfigGet("template.errorUrlPath", "/admin/home/error").(string)
//}

// 全局共享方式缓存template，所有页面的相同template define会冲突并覆盖（优势是共享内存更少）
func (*BsfConfig) GetTemplateLoadHTMLGlobEnabled() bool {
	return CoreUtil.ConfigGet("template.loadHTMLGlob.enabled", false).(bool)
}
func (m *BsfConfig) GetTemplateLoadHTMLGlobMatch() string {
	return CoreUtil.ConfigGet("template.loadHTMLGlob.match", m.GetTemplatePath()+"**/*.html").(string)
}

// 页面共享方式缓存template,每个页面渲染时相同的template define会冲突并覆盖（优势是每个页面维度维护渲染解析，内存会更多）
func (*BsfConfig) GetTemplatePageCacheRenderEnabled() bool {
	return CoreUtil.ConfigGet("template.pageCacheRender.enabled", true).(bool)
}
func (m *BsfConfig) GetTemplatePageCacheRenderShardTemplateFileMatch() string {
	return CoreUtil.ConfigGet("template.pageCacheRender.shardTemplateFileMatch", m.GetTemplatePath()+"_/*.html").(string)
}
func (*BsfConfig) GetTemplateStaticEnabled() bool {
	return CoreUtil.ConfigGet("template.static.enabled", true).(bool)
}
func (*BsfConfig) GetTemplateStaticJsPath() string {
	return CoreUtil.ConfigGet("template.static.jsPath", "web/public/js").(string)
}
func (*BsfConfig) GetTemplateStaticCssPath() string {
	return CoreUtil.ConfigGet("template.static.cssPath", "web/public/css").(string)
}
func (*BsfConfig) GetTemplateStaticImagesPath() string {
	return CoreUtil.ConfigGet("template.static.imagesPath", "web/public/images").(string)
}
func (*BsfConfig) GetTemplateStaticFavicon() string {
	return CoreUtil.ConfigGet("template.static.favicon", "web/public/images/favicon.ico").(string)
}

// log 配置
func (*BsfConfig) GetLogEnabled() bool {
	return CoreUtil.ConfigGet("log.enabled", true).(bool)
}
func (*BsfConfig) GetLogMode() string {
	return CoreUtil.ConfigGet("log.mode", "simple").(string)
}
func (*BsfConfig) GetLogFile() string {
	return CoreUtil.ConfigGet("log.file", "ginApp.log").(string)
}

// redis 配置
func (*BsfConfig) GetRedisAddr() string {
	return CoreUtil.ConfigGet("redis.addr", "127.0.0.1:6379").(string)
}

func (*BsfConfig) GetRedisDb() int {
	return CoreUtil.ConfigGet("redis.db", 0).(int)
}

func (*BsfConfig) GetRedisUsername() string {
	return CoreUtil.ConfigGet("redis.username", "").(string)
}

func (*BsfConfig) GetRedisPassword() string {
	return CoreUtil.ConfigGet("redis.password", "").(string)
}

func (*BsfConfig) GetRedisMinPoolSize() int {
	return CoreUtil.ConfigGet("redis.minPoolSize", 1).(int)
}

func (*BsfConfig) GetRedisMaxPoolSize() int {
	return CoreUtil.ConfigGet("redis.maxPoolSize", 10).(int)
}

// kafka 配置
func (b *BsfConfig) GetKafkaBrokers() string {
	return CoreUtil.ConfigGet("kafka.brokers", "localhost:9092").(string)
}

func (b *BsfConfig) GetKafkaVersion() string {
	return CoreUtil.ConfigGet("kafka.version", "2.5.0").(string)
}

func (b *BsfConfig) GetKafkaGroupID() string {
	return CoreUtil.ConfigGet("kafka.group.id", "default-group").(string)
}

// Producer 相关配置
func (b *BsfConfig) GetKafkaProducerRequiredAck() string {
	return CoreUtil.ConfigGet("kafka.producer.requiredAcks", "WaitForLocal").(string)
}

func (b *BsfConfig) GetKafkaProducerMaxMessageBytes() int {
	return CoreUtil.ConfigGet("kafka.producer.maxMessageBytes", 1000000).(int)
}

func (b *BsfConfig) GetKafkaProducerCompression() string {
	return CoreUtil.ConfigGet("kafka.producer.compression", "none").(string)

}

func (b *BsfConfig) GetKafkaProducerReturnSuccesses() bool {
	return CoreUtil.ConfigGet("kafka.producer.returnSuccesses", true).(bool)
}

// Consumer 相关配置
func (b *BsfConfig) GetKafkaConsumerInitialOffset() string {
	return CoreUtil.ConfigGet("kafka.consumer.initialOffset", "OffsetNewest").(string)
}

//func (b *BsfConfig) GetKafkaConsumerAssignor() string {
//	return ConfigUtil.Get("kafka.consumer.assignor", "range").(string)
//}

func (b *BsfConfig) GetKafkaConsumerReturnErrors() bool {
	return CoreUtil.ConfigGet("kafka.consumer.returnErrors", true).(bool)
}

func (b *BsfConfig) GetKafkaConsumerOffsetsAutoCommitEnable() bool {
	return CoreUtil.ConfigGet("kafka.consumer.offsets.autoCommit.enable", true).(bool)
}

func (b *BsfConfig) GetKafkaConsumerOffsetsAutoCommitIntervalSeconds() time.Duration {
	sec := CoreUtil.ConfigGet("kafka.consumer.offsets.autoCommit.intervalSeconds", 5).(int)
	return time.Duration(sec) * time.Second
}

func (b *BsfConfig) GetKafkaConsumerMaxProcessingTimeSeconds() time.Duration {
	ms := CoreUtil.ConfigGet("kafka.consumer.maxProcessingTimeSeconds", 30).(int)
	return time.Duration(ms) * time.Second
}

// http
func (*BsfConfig) GetHttpMaxIdleConns() int {
	return CoreUtil.ConfigGet("http.maxIdleConns", 200).(int)
}

func (*BsfConfig) GetHttpMaxIdleConnsPerHost() int {
	return CoreUtil.ConfigGet("http.maxIdleConnsPerHost", 100).(int)
}

func (*BsfConfig) GetHttpIdleConnTimeoutSeconds() int {
	return CoreUtil.ConfigGet("http.idleConnTimeoutSeconds", 30).(int)
}

func (*BsfConfig) GetHttpDisableKeepAlives() bool {
	return CoreUtil.ConfigGet("http.disableKeepAlives", false).(bool)
}

func (*BsfConfig) GetHttpSetTimeoutSeconds() int {
	return CoreUtil.ConfigGet("http.setTimeoutSeconds", 60).(int)
}
func (*BsfConfig) GetHttpSetRetryCount() int {
	return CoreUtil.ConfigGet("http.setRetryCount", 3).(int)
}

// token
func (*BsfConfig) GetTokenSecretKey() string {
	return CoreUtil.ConfigGet("token.secretKey", "88888888").(string)
}
func (*BsfConfig) GetTokenTTLSeconds() int {
	return CoreUtil.ConfigGet("token.tTLSeconds", 60*60*24).(int)
}

// swagger
func (*BsfConfig) GetSwaggerEnabled() bool {
	return CoreUtil.ConfigGet("swagger.enabled", true).(bool)
}

// CacheBody 配置
func (m *BsfConfig) GetCacheBodyEnabled() bool {
	return CoreUtil.ConfigGet("cacheBody.enabled", true).(bool)
}

// Cors 配置
func (m *BsfConfig) GetCorsEnabled() bool {
	return CoreUtil.ConfigGet("cors.enabled", true).(bool)
}

// GlobalError 配置
func (m *BsfConfig) GetGlobalErrorEnabled() bool {
	return CoreUtil.ConfigGet("globalError.enabled", true).(bool)
}

// Gzip 配置
func (m *BsfConfig) GetGzipEnabled() bool {
	return CoreUtil.ConfigGet("gzip.enabled", true).(bool)
}

// HttpPrintln 配置
func (m *BsfConfig) GetHttpPrintlnEnabled() bool {
	return CoreUtil.ConfigGet("httpPrintln.enabled", true).(bool)
}
func (m *BsfConfig) GetHttpPrintlnTemplate() string {
	return CoreUtil.ConfigGet("httpPrintln.template2", "status,method,path,query,Token,X-Token,Login-Token,body,timespan").(string)
}

// MaxRequestSize 配置
func (m *BsfConfig) GetMaxRequestSizeEnabled() bool {
	return CoreUtil.ConfigGet("maxRequestSize.enabled", true).(bool)
}
func (*BsfConfig) GetMaxRequestSizeSize() int64 {
	return CoreUtil.ConfigGet("maxRequestSize.size", CoreUtil.ConvertToInt64(10485760)).(int64)
}

// RequestId 配置
func (m *BsfConfig) GetRequestIdEnabled() bool {
	return CoreUtil.ConfigGet("requestId.enabled", true).(bool)
}
